package com.easy.neo4j.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.easy.common.util.Result;
import com.easy.neo4j.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * @author: CoolPro
 * @Desc:
 * @create: 2024-03-15 15:57
 **/
@RestController
@RequestMapping("/station")
@CrossOrigin
public class StationResourceController {

    @Autowired
    StationService stationService; // 修改注入的服务为 StationServiceImpl

    // 获取所有公交车站点信息
    @RequestMapping(value = "/bussite", method = RequestMethod.POST)
    public String getSiteResources(@RequestBody String latLng) {
        JSONObject jsonObject = JSON.parseObject(latLng);
        return stationService.findAllBusStation(jsonObject.getString("lat"), jsonObject.getString("lng"), "1000");
    }

    //获取所有单车点信息
    @RequestMapping(value = "/park",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public String getParkResource(@RequestBody String latLng) {
        JSONObject jsonObject = JSON.parseObject(latLng);
        return stationService.findAllPark(jsonObject.getString("lat"), jsonObject.getString("lng"));
    }

    //根据城市编号获取单车停放点信息
    @RequestMapping(value = "/park/{city}/{page}/{limit}", method = RequestMethod.GET)
    public String getParkByCityCode(@RequestParam(value = "address", defaultValue = "") String address, @PathVariable("city") String cityCode, @PathVariable("page") Integer page, @PathVariable("limit") Integer limit) {
        return stationService.findParkByCityCode(address, cityCode, page, limit);
    }

    //获取共享单车停放点数量
    @RequestMapping(value = "/park/{code}", method = RequestMethod.GET)
    public String getParkCount(@RequestParam(value = "address", defaultValue = "") String address, @PathVariable("code") String cityCode) {
        return stationService.getParkCountByCityCode(address, cityCode);
    }


    //获取地铁信息
    @RequestMapping(value = "/subway")
    public String getSubwayResource(@RequestBody String latLng) {
        JSONObject jsonObject = JSON.parseObject(latLng);
        return stationService.findAllSubway(jsonObject.getString("lat"), jsonObject.getString("lng"));
    }

    //获取线路信息
    @RequestMapping(value = "/busline/{line}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getBusLine(@PathVariable String line) {
        return stationService.findBusLine(line);
    }

    //获取地铁线路信息
    @RequestMapping(value = "/subwayline/{line}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getSubwayLine(@PathVariable String line) {
        return stationService.findSubwayByLineName(line);
    }

    //获取单车停放点的共享单车信息
    @RequestMapping(value = "/bike/{parkName}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getBikeByParkName(@PathVariable String parkName) {
        return stationService.findBikeByParkName(parkName);
    }

    //修改停放点的坐标位置
    @RequestMapping(value = "/park/update", method = RequestMethod.POST)
    public Result<String> updateParkPoint(@RequestBody String data) {
        JSONObject jsonString = JSON.parseObject(data);
        return stationService.updateParkPointByName(jsonString.getString("parkName"), jsonString.getString("lat"), jsonString.getString("lng"));
    }

    //创建单车投放点
    @RequestMapping(value = "/park/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Result<String> addPark(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        System.out.println(data);
        return stationService.addPark(
                jsonObject.getString("address"),
                jsonObject.getString("cityCode"),
                jsonObject.getString("reginId"),
                jsonObject.getString("lat"),
                jsonObject.getString("lng"),
                jsonObject.getString("parkName"));
    }

    @PostMapping("/park/relationship")
    public Result<String> createNearbyRelationships(@RequestBody String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        return stationService.createNearbyRelationships(jsonObject.getString("address"));
    }
}